<!-- top-tips 组件 -->
<template>
  <div class="top-tips" v-show="visible">
    <slot>{{content}}</slot>
  </div>
</template>

<script>
export default {
  name: 'top-tips',
  props: {
    content: String,
    duration: {
      type: Number,
      default: 2000
    },
    value: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    visible: {
      get () {
        return this.value
      },
      set (val) {
        this.$emit('input', val)
      }
    }
  },
  methods: {
    hide () {
      if (this.duration) {
        this.visible = false
      }
    }
  },
  watch: {
    value (val) {
      if (this.duration && val) {
        let timer = setTimeout(() => {
          this.hide()
          this.$emit('close')
          clearTimeout(timer)
        }, this.duration)
      }
    }
  }
}
</script>

<style lang="scss">
.top-tips {
  position: fixed;
  transform: translateZ(0);
  top: 0;
  left: 0;
  right: 0;
  padding: 8px;
  font-size: 30px;
  text-align: center;
  color: #fff;
  z-index: 5000;
  word-wrap: break-word;
  word-break: break-all;
  background-color: rgba(255, 30, 0, 1);
}
</style>
